<ul class="breadcrumb">
    <li>
        <a href="/">首页</a> <span class="divider">/</span>
    </li>
    <li>
        <a href="#">系统管理</a> <span class="divider">/</span>
    </li>
    <li class="active">
        权限管理
    </li>
</ul>

<table class="table table-bordered" style="margin-top: 12px;">
<thead>
    <tr>
        <th style="width: 200px;">权限名</th>
        <th>模块 Module</th>
        <th>控制器 Controller</th>
        <th>方法 Action</th>
        <th>操作</th>
    </tr>
</thead>
<tbody>
<? if ($scanList): ?>
<? foreach ($scanList as $module => $controllers): ?>
    <? if ($controllers): ?>
        <? foreach ($controllers as $controller => $methods): ?>
            <? if ($methods): ?>
                <? foreach ($methods as $value): ?>
                    <tr>
                        <?
                            // 该权限是否已激活（已入库）
                            if (isset($privList[$controller][$value['action']])) {
                                $privRow = $privList[$controller][$value['action']];
                                $value['title'] = $privRow['priv_name'];
                            } else {
                                $privRow = array();
                            }
                        ?>
                        <td rel="priv_name">
                            <input type="text" <? if ($privRow): ?> onchange="_privilege.update('<?=$privRow['priv_id']?>', this.value)" <? endif; ?> value="<?=$value['title']?>" class="input-medium" style="margin-bottom: 0" />
                        </td>
                        <td rel="module"><?=$module?></td>
                        <td rel="controller"><?=$controller?></td>
                        <td rel="action"><?=$value['action']?></td>
                        <td style="width: 65px; text-align: center;">
                        <? if ($privRow && $privRow['priv_id']): ?>
                            <button class="btn" onclick="_privilege.remove('<?=$privRow['priv_id']?>', this)" data-loading-text="加载中">禁 &nbsp; 用</button>
                        <? else: ?>
                            <button class="btn btn-primary" onclick="_privilege.add(this)" data-loading-text="加载中">启 &nbsp; 用</button>
                        <? endif; ?>
                        </td>
                    </tr>
                <? endforeach; ?>
            <? endif; ?>
        <? endforeach; ?>
    <? endif; ?>
<? endforeach; ?>
<? else: ?>
    <tr><td colspan="20">没有符合条件的记录</td></tr>
<? endif; ?>
</tbody>
</table>

<script type="text/javascript">
var _privilege =
{
    // 启用一个权限
    add : function (btnObj)
    {
        var tr = $(btnObj).closest('tr');

        // 检测权限名
        privNameInputObj = tr.find('td[rel=priv_name] input');
        var privName = $.trim(privNameInputObj.val());
        if (privName == '') {
            privNameInputObj.addClass('input-error').attr('placeHolder', '请输入权限名');
            return false;
        }

        var module     = $.trim(tr.find('td[rel=module]').html());
        var controller = $.trim(tr.find('td[rel=controller]').html());
        var action     = $.trim(tr.find('td[rel=action]').html());

        // 提交数据
        $.post('/_system/privilege/add', {
            'priv_name'  : privName,
          //  'module'     : module,
            'controller' : controller,
            'action'     : action
        }, function(data){
            if (data.res == 'success') {
                // 将按钮禁用
                $(btnObj).removeClass('btn-primary').addClass('disabled').html('已启用').removeAttr('onclick').unbind('click');
                // 将输入框禁用
                privNameInputObj.removeClass('input-error').addClass('input-disabled').prop('disabled', true);
            } else {
                alert(data.msg);
            }
        }, 'json');
    },

    // 禁用一个权限
    remove : function(privId, btnObj)
    {
        if (!confirm('确定要禁用并移除这个权限颗粒吗？\n注意：禁用后如果需要再重新启用，则角色组需要重新关联该权限。')) {
            return false;
        }

        var tr = $(btnObj).closest('tr');
        privNameInputObj = tr.find('td[rel=priv_name] input');

        // 提交数据
        $.post('/_system/privilege/delete', {
            'priv_id'  : privId
        }, function(data){
            if (data.res == 'success') {
                // 将按钮禁用
                $(btnObj).addClass('disabled').html('已禁用').removeAttr('onclick').unbind('click');
                // 将输入框禁用
                privNameInputObj.removeClass('input-error').addClass('input-disabled').prop('disabled', true);
            } else {
                alert(data.msg);
            }
        }, 'json');
    },

    // 重命名一个已入库的权限名
    update : function(privId, privName)
    {
        $.post('/_system/privilege/update', {
            'priv_id'   : privId,
            'priv_name' : privName
        }, function(data){
            if (data.res != 'success') {
                alert(data.msg);
            }
        }, 'json');
    }
}
</script>